<?PHP
/**
 * @author Jongwon Byun <blumine@paran.com>
 */

class Search_m extends CI_Model {

// 	const field1 = " CUSTOMER_CODE as no, CUSTOMER_GROUP_CODE contents, LAST_NAME subject, FIRST_NAME user_name, LOGIN_ID user_id, EMAIL hit, BIRTH_DATE voted_count, SEX reply_count, LOGIN_DATETIME reg_date";
// 	const field2 = " COMMODITY_CODE as no, COMMODITY_NAME contents, COMMODITY_TYPE subject, REPRESENT_SKU_CODE user_name, REPRESENT_SKU_UNIT_PRICE user_id, STOCK_STATUS_NO hit, STOCK_MANAGEMENT_TYPE voted_count, VOLUME_DISCOUNT_NO reply_count, SHORT_DESCRIPTION reg_date";
// 	const field3 = " COMMODITY_CODE as no, SKU_CODE contents, UNIT_PRICE subject, DISCOUNT_PRICE user_name, RESERVATION_PRICE user_id, CHANGE_UNIT_PRICE hit, JAN_CODE voted_count, DISPLAY_ORDER reply_count, STANDARD_DETAIL1_NAME reg_date";
// 	const field4 = " CATEGORY_CODE as no, CATEGORY_NAME_PC contents, CATEGORY_NAME_MOBILE subject, PARENT_CATEGORY_CODE user_name, PATH user_id, DEPTH hit, DISPLAY_ORDER voted_count, COMMODITY_COUNT_PC reply_count, LAST_RELATED_COUNT_DATETIME reg_date";
// 	const field5 = " SHOP_CODE as no, CATEGORY_CODE contents, COMMODITY_CODE subject, DISPLAY_ORDER user_name, MAIN_FLG user_id, ORM_ROWID hit, CREATED_USER voted_count, CREATED_DATETIME reply_count, UPDATED_DATETIME reg_date";




    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
        $this->table = '';
    }

	//통합검색 기능추가 by emc (2009/08/19)
	function search_list($page, $rp, $post)
	{
		$field1 = " CUSTOMER_CODE as no, CUSTOMER_GROUP_CODE contents, LAST_NAME subject, FIRST_NAME user_name, LOGIN_ID user_id, EMAIL hit, BIRTH_DATE voted_count, SEX reply_count, LOGIN_DATETIME reg_date";
		$field2 = " COMMODITY_CODE as no, COMMODITY_NAME contents, COMMODITY_TYPE subject, REPRESENT_SKU_CODE user_name, REPRESENT_SKU_UNIT_PRICE user_id, STOCK_STATUS_NO hit, STOCK_MANAGEMENT_TYPE voted_count, VOLUME_DISCOUNT_NO reply_count, SHORT_DESCRIPTION reg_date";
		$field3 = " COMMODITY_CODE as no, SKU_CODE contents, UNIT_PRICE subject, DISCOUNT_PRICE user_name, RESERVATION_PRICE user_id, CHANGE_UNIT_PRICE hit, JAN_CODE voted_count, DISPLAY_ORDER reply_count, STANDARD_DETAIL1_NAME reg_date";
		$field4 = " CATEGORY_CODE as no, CATEGORY_NAME_PC contents, CATEGORY_NAME_MOBILE subject, PARENT_CATEGORY_CODE user_name, PATH user_id, DEPTH hit, DISPLAY_ORDER voted_count, COMMODITY_COUNT_PC reply_count, LAST_RELATED_COUNT_DATETIME reg_date";
		$field5 = " SHOP_CODE as no, CATEGORY_CODE contents, COMMODITY_CODE subject, DISPLAY_ORDER user_name, MAIN_FLG user_id, ORM_ROWID hit, CREATED_USER voted_count, CREATED_DATETIME reply_count, UPDATED_DATETIME reg_date";


		$where1 = " WHERE (CUSTOMER_CODE  like \"%".$post['s_word']."%\" or CUSTOMER_GROUP_CODE like \"%".$post['s_word']."%\" or LOGIN_ID like \"%".$post['s_word']."%\")  ";
		$where2 = " WHERE (COMMODITY_SEARCH_WORDS like \"%".$post['s_word']."%\" or SHADOW_SEARCH_WORDS like \"%".$post['s_word']."%\") ";
		$where3 = " WHERE (COMMODITY_CODE like \"%".$post['s_word']."%\" or STANDARD_DETAIL1_NAME like \"%".$post['s_word']."%\" or STANDARD_DETAIL2_NAME like \"%".$post['s_word']."%\") ";
		$where4 = " WHERE (CATEGORY_CODE  like \"%".$post['s_word']."%\" or PATH like \"%".$post['s_word']."%\")  ";
		$where5 = " WHERE (CATEGORY_CODE like \"%".$post['s_word']."%\" or CATEGORY_SEARCH_PATH like \"%".$post['s_word']."%\") ";

		//$field = " no, contents, subject, user_name, user_id, hit, voted_count, reply_count, reg_date ";
       // $where = " WHERE (subject like \"%".$post['s_word']."%\" or contents like \"%".$post['s_word']."%\") and is_delete = 'N' and original_no = '0' ";

        $sql = "SELECT 'TEST' nickname, a.table, a.tbn, a.no, a.contents, a.subject, a.user_name, a.user_id, a.hit, a.voted_count, a.reply_count, a.reg_date from ( ";
        $sql.= "(SELECT '고객' as 'table', 'CUSTOMER' as 'tbn', ".$field1." FROM CUSTOMER ".$where1.") UNION ";
        $sql.= "(SELECT '상품' as 'table', 'COMMODITY_HEADER ' as 'tbn', ".$field2." FROM COMMODITY_HEADER  ".$where2.") UNION ";
        $sql.= "(SELECT '상품 상세' as 'table', 'COMMODITY_DETAIL ' as 'tbn', ".$field3." FROM COMMODITY_DETAIL ".$where3.") UNION ";
        $sql.= "(SELECT '카테고리'     as 'table', 'CATEGORY ' as 'tbn', ".$field4." FROM CATEGORY  ".$where4.") UNION ";
        $sql.= "(SELECT '카테고리 상품'    as 'table', 'CATEGORY_COMMODITY ' as 'tbn', ".$field5." FROM CATEGORY_COMMODITY  ".$where5.") ";


        $sql.= " ) as a ";
        		 //, users b where a.user_id=b.userid ";
        $sql.= "order by reg_date desc ";
		$sql.= 'limit '.$page.', '.$rp;
		//echo $sql; exit;
		$rs = $this->db->query($sql);
        return $rs->result_array();
	}

	//통합검색 기능
	function search_total($post)
	{

		//print_r($post);
		/*
		if ($post) {
			$this->db->like($post["method"], $post["s_word"]);
  		}
  		$this->db->where(array('is_delete'=>'N', 'original_no'=>'0'));

		$query = $this->db->get($this->table);
		*/

		$field1 = " CUSTOMER_CODE as no, CUSTOMER_GROUP_CODE contents, LAST_NAME subject, FIRST_NAME user_name, LOGIN_ID user_id, EMAIL hit, BIRTH_DATE voted_count, SEX reply_count, LOGIN_DATETIME reg_date";
		$field2 = " COMMODITY_CODE as no, COMMODITY_NAME contents, COMMODITY_TYPE subject, REPRESENT_SKU_CODE user_name, REPRESENT_SKU_UNIT_PRICE user_id, STOCK_STATUS_NO hit, STOCK_MANAGEMENT_TYPE voted_count, VOLUME_DISCOUNT_NO reply_count, SHORT_DESCRIPTION reg_date";
		$field3 = " COMMODITY_CODE as no, SKU_CODE contents, UNIT_PRICE subject, DISCOUNT_PRICE user_name, RESERVATION_PRICE user_id, CHANGE_UNIT_PRICE hit, JAN_CODE voted_count, DISPLAY_ORDER reply_count, STANDARD_DETAIL1_NAME reg_date";
		$field4 = " CATEGORY_CODE as no, CATEGORY_NAME_PC contents, CATEGORY_NAME_MOBILE subject, PARENT_CATEGORY_CODE user_name, PATH user_id, DEPTH hit, DISPLAY_ORDER voted_count, COMMODITY_COUNT_PC reply_count, LAST_RELATED_COUNT_DATETIME reg_date";
		$field5 = " SHOP_CODE as no, CATEGORY_CODE contents, COMMODITY_CODE subject, DISPLAY_ORDER user_name, MAIN_FLG user_id, ORM_ROWID hit, CREATED_USER voted_count, CREATED_DATETIME reply_count, UPDATED_DATETIME reg_date";


		$where1 = " WHERE (CUSTOMER_CODE  like \"%".$post['s_word']."%\" or CUSTOMER_GROUP_CODE like \"%".$post['s_word']."%\" or LOGIN_ID like \"%".$post['s_word']."%\")  ";
		$where2 = " WHERE (COMMODITY_SEARCH_WORDS like \"%".$post['s_word']."%\" or SHADOW_SEARCH_WORDS like \"%".$post['s_word']."%\") ";
		$where3 = " WHERE (COMMODITY_CODE like \"%".$post['s_word']."%\" or STANDARD_DETAIL1_NAME like \"%".$post['s_word']."%\" or STANDARD_DETAIL2_NAME like \"%".$post['s_word']."%\") ";
		$where4 = " WHERE (CATEGORY_CODE  like \"%".$post['s_word']."%\" or PATH like \"%".$post['s_word']."%\")  ";
		$where5 = " WHERE (CATEGORY_CODE like \"%".$post['s_word']."%\" or CATEGORY_SEARCH_PATH like \"%".$post['s_word']."%\") ";

		$sql = "SELECT 'TEST' nickname, a.table, a.tbn, a.no, a.contents, a.subject, a.user_name, a.user_id, a.hit, a.voted_count, a.reply_count, a.reg_date from ( ";
		$sql.= "(SELECT '고객' as 'table', 'CUSTOMER' as 'tbn', ".$field1." FROM CUSTOMER ".$where1.") UNION ";
		$sql.= "(SELECT '상품' as 'table', 'COMMODITY_HEADER ' as 'tbn', ".$field2." FROM COMMODITY_HEADER  ".$where2.") UNION ";
		$sql.= "(SELECT '상품 상세' as 'table', 'COMMODITY_DETAIL ' as 'tbn', ".$field3." FROM COMMODITY_DETAIL ".$where3.") UNION ";
		$sql.= "(SELECT '카테고리'     as 'table', 'CATEGORY ' as 'tbn', ".$field4." FROM CATEGORY  ".$where4.") UNION ";
		$sql.= "(SELECT '카테고리 상품'    as 'table', 'CATEGORY_COMMODITY ' as 'tbn', ".$field5." FROM CATEGORY_COMMODITY  ".$where5.") ";
		$sql.= " ) as a ";
        $sql.= "order by reg_date desc ";
//echo $sql;
		$rs = $this->db->query($sql);
		//return $rs->result();

        return $rs->num_rows();
	}
}

?>
